﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;

using DotSpatial.Data;

namespace HAGISV2
{
    /// <summary>
    /// 小刘写
    /// </summary>
    [Serializable]
    public class ImageLayer : Layer       
    {        
        public ImageLayer(string id,string originalFileName)
            : base(id)
        {
            this._originalFileName = originalFileName;
            this._firstLoad = true;
            this._fileName = System.IO.Path.GetFileName(this._originalFileName);
           
            this._isSelectable = false;
            
        }

        //文件原始路径
        private string _originalFileName = "";
       
        //文件是否第一次加载
        private bool _firstLoad = false;      

        //文件名称
        private string _fileName;

        [NonSerialized]
        private InRamImageData _dataset;
        
        [NonSerialized]
        private DotSpatial.Controls.IMapImageLayer _iLayer;


        # region 继承的方法

        public override Extent GetLayerExtent()
        {
            if (!this.IsLoaded || this._viewMode != FMAPVIEW.NORMAL)
            {
                return null;
            }
            return this._iLayer.Extent;
        }

        public override void Render()
        {
            base.Render();

            if (this._firstLoad == true)
            {
                this._firstLoad = false;  //第一次加载后，此字段便永远变成false了……      

                if (System.IO.File.Exists(this._originalFileName) == false)
                {
                    return;
                }
                System.IO.File.SetAttributes(this._originalFileName, System.IO.FileAttributes.Normal);
                if (System.IO.File.Exists(this.DefaultManager.layerPath + this._fileName) == false)
                {
                    System.IO.File.Copy(this._originalFileName, this.DefaultManager.layerPath + this._fileName);
                }

                this._dataset = new InRamImageData(this.DefaultManager.layerPath + this._fileName);
                this._dataset.WorldFile.TopLeftX = this._topLeftX;
                this._dataset.WorldFile.TopLeftY = this._topLeftY;
                this._dataset.WorldFile.CellWidth = this._cellWidth;
                this._dataset.WorldFile.CellHeight = this._cellHeight;
                //第一次加载WorldFile是由外部设置的
                this._originalFileName = "";
            }
            else
            {
                this._dataset = new InRamImageData(this.DefaultManager.layerPath + this._fileName);
                //this._dataset.WorldFile.Open();    //坐标文件存在的话会自动加载
            }

            this._dataset.Projection = this.DefaultManager.CurrentMap.Projection;
            this._iLayer = this.DefaultManager.CurrentMap.Layers.Add(this._dataset);

            if (this._mapLayerIndex != -1)
            {
                this._iLayer.LockDispose();

                this.DefaultManager.CurrentMap.Layers.Remove(this._iLayer);

                this.DefaultManager.CurrentMap.Layers.Insert(this._mapLayerIndex, this._iLayer);

                this._iLayer.UnlockDispose();

                this._mapLayerIndex = -1;
            }

            this._isLoaded = true;

            this.OnSetVisible();
        }

        public override void StopRender()
        {
            this.DefaultManager.CurrentMap.Layers.Remove(this._iLayer);
            this._isLoaded = false;
        }

        public override bool SaveLayer()
        {
            try
            {
                this._dataset.SaveAs(this.DefaultManager.layerPath + this._fileName);
                //this._dataset.WorldFile.Save();  //坐标文件会自动保存
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public override bool OpenLayer()
        {
            this._dataset = new InRamImageData();
            this.Render();
            return true;
        }


        protected override void OnSetVisible()
        {
            base.OnSetVisible();

            if (!this.IsLoaded)
            {
                return;
            }

            if (this._visiable == true && this._viewMode == FMAPVIEW.NORMAL)
            {
                this._iLayer.IsVisible = true;
            }
            else
            {
                this._iLayer.IsVisible = false;
            }
           
        }

        protected override void OnViewModeChanged()
        {
            base.OnViewModeChanged();
            this.OnSetVisible();
        }


        # endregion


        # region 设置参数


        private double _topLeftX = 0;
        private double _topLeftY = 0;
        private double _cellWidth = 10;
        private double _cellHeight = 10;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="width">位图像素宽</param>
        /// <param name="height">位图像素高</param>
        /// <param name="absLeftBottomPt">位图加载位置的左下角</param>
        /// <param name="absRightUpPt">位图加载位置的右上角</param>
        public void SetWorldFile(PointF absLeftBottomPt,PointF absRightUpPt,int width,int heigth)
        {

            this._topLeftX = absLeftBottomPt.X;
            this._topLeftY = absRightUpPt.Y;
            this._cellWidth = (absRightUpPt.X - absLeftBottomPt.X) / width;
            this._cellHeight = -(absRightUpPt.Y - absLeftBottomPt.Y) / heigth;            

            //this._dataset.WorldFile.TopLeftX = absLeftBottomPt.X;
            //this._dataset.WorldFile.TopLeftY = absRightUpPt.Y;
            //this._dataset.WorldFile.CellWidth = (absRightUpPt.X - absLeftBottomPt.X) / this._dataset.Width;
            //this._dataset.WorldFile.CellHeight = -(absRightUpPt.Y - absLeftBottomPt.Y) / this._dataset.Height;            
        }

        # endregion

    }
}
